home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 988 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.4 KB  |  77 lines

  1. Path: library.erc.clarkson.edu!rpi!not-for-mail
  2. From: floydb1@lib108.its.rpi.edu (Barry B Floyd)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Creating a pointer to a function "void (*ptrFunction)()"  inside a class
  5. Date: 8 Jan 1996 14:22:59 -0500
  6. Organization: Rensselaer Polytechnic Institute, Troy, NY.
  7. Message-ID: <4crquj$7t1@lib108.its.rpi.edu>
  8. References: <30ECA10F.3D99@ifu.net> <NITIN.96Jan5125830@more.eng.sun.com>
  9. NNTP-Posting-Host: lib108.its.rpi.edu
  10. X-newsreader: xrn 7.04-beta-11
  11.  
  12.  
  13. In article <NITIN.96Jan5125830@more.eng.sun.com>, nitin@more.eng.sun.com (Nitin More [CONTRACTOR]) writes:
  14. |> In article <30ECA10F.3D99@ifu.net> Jason Gresh <gresh@ifu.net> writes:
  15. |> >   class BaseClass
  16. |> >   {
  17. |> >       int (*ptrFunction)();
  18. |> >   }
  19. |> >
  20. |> >   class DerivedClass : BaseClass
  21. |> >   {
  22. |> >       DerivedClass::DerivedClass();
  23. |> >       int myFunction(int, int);
  24. |> >   }
  25. |> >
  26. |> >   DerivedClass::DerivedClass()
  27. |> >   {
  28. |> >       ptrFunction = myFunction;
  29. |> >   }
  30. |> >
  31. |> >   DerivedClass::myFunction(int, int)
  32. |> >   {
  33. |> >   // code ....
  34. |> >   }
  35. |> >
  36.  
  37. Try:
  38.  
  39.     class BaseClass
  40.     {
  41.         ...                 // other stuff
  42.     virtual    int Function ( int, int ) = 0 ;  // pure virtual fn()
  43.         ...                 // other stuff
  44.     }
  45.  
  46.     class DerivedClass : BaseClass
  47.     {
  48.         ...                 // other stuff
  49.         int Function ( int, int )      { // your code    } ;
  50.         ...                 // other stuff
  51.     }
  52.  
  53. If DerivedClass does not declare "Function ( int, int )" you will
  54. get a compiler error. All classes derived from BaseClass will 
  55. essentially be required to provide a "Function ( int, int )".
  56. However, I believe you will NOT be able to create an instance of
  57. BaseClass (i.e. BaseClass a_base ; ) because of the pure virtual
  58. function. This should be OK given your initial request.
  59.  
  60. Generally, the "= 0" keeps you honest, especially useful with derived
  61. classes containing many many functions or containing functions with
  62. the same name but differing parameter lists and/or return values.
  63.  
  64. barry
  65.  
  66. ps
  67.  
  68. note: "Function ( int, int )" remains virtual in DerivedClass,
  69. even though the keyword "virtual" is not included in the declaration.
  70. Thus, I believe "class DerivedDerivedClass : DerivedClass" must provide
  71. a "Function ( int, int )" as well.
  72. -- 
  73. +--------------------------------------------------------------------+ 
  74. | Barry B. Floyd                   \\\               floydb1@rpi.edu |
  75. | RPI Alum. '84 '87 '88              \\\                             |
  76. +--------------------------------------------------------------------+
  77.